package com.cqan.account.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cqan.account.entity.Department;
import com.cqan.account.service.DepartmentService;
import com.cqan.common.ViewName;
import com.cqan.controller.BaseController;
import com.cqan.exception.AppException;

@Controller
@RequestMapping("/department")
public class DepartmentController extends BaseController<Department,Long, DepartmentService> {

	@RequestMapping(value="/add",method=RequestMethod.GET)
	public ModelAndView add(HttpServletRequest req, HttpServletResponse resp)
			throws AppException {
		Department department1 = new Department();
		Department department2 = new Department();
		Department department3 = new Department();
		Department department4 = new Department();
		Department department5 = new Department();
		Department department6 = new Department();
		Department department7 = new Department();
		Department department8 = new Department();
		department1.setName("总公司");
		department2.setName("北京分公司");
		department3.setName("长沙分司");
		department4.setName("湖南分公司");
		department5.setName("财务部");
		department6.setName("研发部");
		department7.setName("人事部");
		department8.setName("行政部");
		department1.setEnabled(true);
		department2.setEnabled(true);
		department3.setEnabled(true);
		department4.setEnabled(true);
		department5.setEnabled(true);
		department6.setEnabled(true);
		department7.setEnabled(true);
		department8.setEnabled(true);
		entityService.add(department1);
		entityService.add(department2);
		ModelAndView view = new ModelAndView(ViewName.DEPARTMENT_INDEX);
		return view;
	}

	
	@RequestMapping(value="/index",method=RequestMethod.GET)
	public ModelAndView list(HttpServletRequest req, HttpServletResponse resp)
			throws AppException {
		
		return null;
	}

	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public ModelAndView delete(HttpServletRequest req, HttpServletResponse resp)
			throws AppException {
		
		return null;
	}

	@RequestMapping(value="/update",method=RequestMethod.POST)
	public ModelAndView update(HttpServletRequest req, HttpServletResponse resp)
			throws AppException {
		
		return null;
	}
	
	@ResponseBody 
	@RequestMapping(value="/getDepartmentByPid")
	public String getDepartmentByPid(@RequestParam("pid")Long pid){
		if (pid==null) {
			pid=0L;
		}
		List<Department> data = entityService.getDepartmentByPid(pid);
		JSONArray array = new JSONArray();
		for (Department department : data) {
			JSONObject obj = changOrgToJson(department);
			array.add(obj);
		}
		return array.toString();
	}
	
	public JSONObject changOrgToJson(Department org){
		if(org!=null){
			JSONObject	obj = new JSONObject();
			obj.accumulate("id",org.getId());
			obj.accumulate("text",org.getName());
			obj.accumulate("state","closed");
			if (org.getChildren()!=null&&org.getChildren().size()>0) {
				JSONArray array = new JSONArray();
				for (Department child : org.getChildren()) {
					array.add(changOrgToJson(child));
				}
				obj.accumulate("children", array);
			}
			return obj;
		}
		return null;
	}

	@Override
	@Autowired
	public void setEntityService(DepartmentService entityService) {
		
		this.entityService = entityService;
	}

}
